<?php
/**
 * Created by PhpStorm.
 * User: zhana0
 * Date: 18-3-12
 * Time: 下午3:59
 */

namespace App\Http\Controllers;
use App\Jobs\sendMessage;
use App\Jobs\sendOfflineMessage;
use App\Model\Messages;
use App\Model\Tokens;
use App\Model\Users;
use Illuminate\Routing\Controller as BaseController;
use WebSocket\Client;


class MessageController extends BaseController
{
    public $redis;
    public $ws;
    public $user;
    public function __construct()
    {
        $this->redis = new \Redis();
        $this->user = new Users();
        $this->redis->connect('127.0.0.1',6379);
        $this->ws = new Client('ws://127.0.0.1:1024');
    }

    /**
     * 发送消息接口
     */
    public function send()
    {
        try {

            $token = request('token');
            $friend_name = request('friend_name');
            $msg = request('msg');

            //验证token
            if ($token = Tokens::verify_token($token)) {

                $from_user = $this->user->find($token->user_id);
                $to_user = $this->user->where(['user_name'=>$friend_name])->first();

                if (!$to_user) {
                    throw new \Exception('发送失败');
                }

                $msg = Messages::add([
                    'msg_type'=>'text',
                    'content'=>$msg,
                    'from'=>$from_user->id,
                    'to'=>$to_user->id,
                    'type'=>'user',
                    'status'=>'0'
                ]);

                //判断好友在线状态
                $to_user_status = $this->redis->get($to_user->id);
                if (!$to_user_status) {
                    throw new \Exception('好友未登录，上线后可收到消息');
                } else {
                    //添加消息队列
                    sendMessage::dispatch($from_user,$to_user,$msg);
                    return response()->json(['code'=>1,'msg'=>'发送成功']);
                }

            } else {
                throw new \Exception('token验证失败');
            }


        }catch (\Exception $e)
        {
            return response()->json(['code'=>0,'msg'=>$e->getMessage()]);
        }
    }

    /**
     * 绑定客户端与用户id接口
     */
    public function bind_client()
    {
        try {

            $token = request('token');
            $client_id = request('client_id');

            if ($token = Tokens::verify_token($token)) {

                //向websocket服务发送绑定请求
                $this->ws->send(json_encode([
                    'type'=>'bind_client',
                    'client_id'=>$client_id,
                    'user_id'=>$token->user_id
                ]));
                $this->ws->close();
                //添加离线消息队列
                sendOfflineMessage::dispatch($token->user_id);
                return response()->json(['code'=>1,'msg'=>'登陆成功']);
            } else {
                throw new \Exception('token验证失败');
            }
        } catch (\Exception $exception)
        {
            return response()->json(['code'=>0,'msg'=>$exception->getMessage()]);
        }
    }
}